var express = require('express');
var router = express.Router();
var con = require('dao/dbConnect');
var util = require('util');

/* GET home page. */
router.get('/', function(req, res, next) {
  client = con.connect();
  user = req.session.user;
  client.query("SELECT ug.group_id groupId,g.group_name groupName from base_user_group ug,base_group_info g where user_id = '"+user.id+"' and ug.group_id = g.id", 
    function(err, results, fields){
      var groupList = [];
      for(var i in results){
        var groupObj = new Object();
        groupObj.groupName = results[i].groupName;
        groupObj.groupId = results[i].groupId;
        groupList[i] = groupObj;
      }
      res.render('random-food', { groupList: results});
  });
});

router.get('/getItem', function(req, res, next) {
  client = con.connect();
  var groupId = req.query.groupId;
  client.query("select item_name itemName from base_group_item where group_id = ?", [groupId],
    function(err, results, fields){
      var index = parseInt(Math.random()*results.length);
      res.send(results[index]);
  });
});

module.exports = router;
